<?php

namespace app\api\controller\paotui\poster;

use addons\paotui\service\Poster;
use app\admin\model\paotui\PosterLocation;
use think\Exception;

/**
 * 快递接口
 */
class Post extends PosterApi
{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = "*";

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 快递列表
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index(){
        $status = $this->request->post("type",'');  // 1待抢单 2待揽收 3运输中 4派送中 5已完成 6服务中（2,3,4） -1已取消
        $page = $this->request->post('page',0);
        $limit = $this->request->post('limit',0);

        $where['poster_id'] = $this->auth->id;

        //获取当前快递员的最近的经纬度
        $posterLocationModel = new PosterLocation();
        $posterLocationInfo = $posterLocationModel->where(['poster_id'=>$this->auth->id])->find();
        $lat = empty($posterLocationInfo['lat'])?0:$posterLocationInfo['lat'];
        $lng = empty($posterLocationInfo['lat'])?0:$posterLocationInfo['lng'];

        $postService = new \addons\paotui\service\Post();
        $data = $postService->getPosterPostList($status,$where,$page,$limit,"*",$lng,$lat,$this->auth->id);

        $this->success('请求成功',$data);
    }

    /**
     * 拒单
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function refuse(){
        $order_no = $this->request->post('order_no',"");
        $postService = new \addons\paotui\service\Post();
        if($postInfo =  $postService->getInfoByPoster($order_no,$this->auth->id)){
            \addons\paotui\service\Post::refusePost($postInfo['id'],$this->auth->id);
            $this->success("请求成功",$postInfo);
        }else{
            $this->error($postService->error);
        }
    }

    /**
     * 接受派单
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function accept(){
        $order_no = $this->request->post('order_no',"");
        $postService = new \addons\paotui\service\Post();
        if($postInfo =  $postService->getInfoByPoster($order_no,$this->auth->id)){
            $poster = $this->auth->getPoster();
            \addons\paotui\service\Post::acceptPost($postInfo['id'],$poster);
            $this->success("请求成功",$postInfo);
        }else{
            $this->error($postService->error);
        }
    }

    /**
     * 获取订单详情
     */
    public function info(){
        $order_no = $this->request->post('order_no',"");
        if(empty($order_no)){
            $this->error("参数异常");
        }

        $postService = new \addons\paotui\service\Post();
        if($postInfo =  $postService->getInfoByPoster($order_no,$this->auth->id)){
            $this->success("请求成功",$postInfo);
        }else{
            $this->error($postService->error);
        }
    }

    /**
     * 快递员抢单
     */
    public function beat(){
        $order_no = $this->request->post('order_no',"");
        if(empty($order_no)){
            $this->error("参数异常");
        }
        $poster = $this->auth->getPoster();
        $postService = new \addons\paotui\service\Post();
        if($postService->beatPost($order_no,$poster)){
            $this->success("抢单成功");
        }else{
            $this->error($postService->error);
        }
    }

    /**
     * 取件
     */
    public function collect(){
        $order_no = $this->request->post('order_no',"");
        $images = $this->request->post('images');
        if(empty($order_no)){
            $this->error("参数异常");
        }

        $poster = $this->auth->getPoster();
        $postService = new \addons\paotui\service\Post();
        if($postService->collectPost($order_no,$poster,$images)){
            $this->success("取件成功");
        }else{
            $this->error($postService->error);
        }

    }

    /**
     * 送达
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function reach(){
        $order_no = $this->request->post('order_no',"");
        $image = $this->request->post('image');

        $lnglat = $this->request->post('lnglat');
        if(empty($order_no)){
            $this->error("参数异常");
        }

        $poster = $this->auth->getPoster();

        $postService = new \addons\paotui\service\Post();
        if($postService->reachPost($order_no,$poster,$lnglat,$image)){
            //计算快递员佣金
            $posterService = new Poster();
            $posterService->brokerage($order_no);

            $this->success("送达成功");
        }else{
            $this->error($postService->error);
        }
    }

}
